package com.example.chenhaosblog.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.example.chenhaosblog.base.BaseController;
import com.example.chenhaosblog.service.RegisterService;
import com.example.chenhaosblog.utils.DataUtil;
import com.example.chenhaosblog.utils.DateUtil;
import com.example.chenhaosblog.utils.FileUpload;
import com.example.chenhaosblog.utils.PageData;
import com.example.chenhaosblog.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/registerController")
public class RegisterController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(RegisterController.class);

	@Autowired
	private RegisterService registerService;
	
	@Value("${imgUplode.url}")
	private String upimg;//图片上传路径
	@Value("${getImg.url}")
	private String getimg;//图片访问路径
	/**
	 * 注册用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addRegister")
	@ResponseBody
	public PageData addRegister() throws Exception {
		PageData resulData = new PageData();// 返回结果
		PageData pageData = getPageData();// 参数
		try {
			Map<String, Object> pageMap = UuidUtil.getPassword(pageData.getString("password"), pageData.getString("username"));
			pageData.put("isDelete", 0);
			pageData.put("salt", pageMap.get("uuid"));
			pageData.put("password", pageMap.get("password"));
			pageData.put("typeId", "0");
			pageData.put("createTime", DateUtil.getTime());
			pageData.put("updateTime", DateUtil.getTime());
			pageData.put("status", 0);
			
			PageData registerData = registerService.findShow(pageData);
			if (CollectionUtils.isEmpty(registerData)) {
				pageData.put("id", get32UUID());
				registerService.addRegister(pageData);
				resulData.put("code", "00");
				resulData.put("data", "");
				resulData.put("msg", "保存成功");
			}else {
				resulData.put("code", "01");
				resulData.put("data", "");
				resulData.put("msg", "该账号已存在");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new Exception(e.getMessage());
		}
		return resulData;
	}

	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteUser")
	@ResponseBody
	public PageData deleteUser() throws Exception {
		PageData resultData = new PageData();
		PageData pageData = getPageData();
		try {
			registerService.deleteUser(pageData);
			resultData.put("code", "00");
			resultData.put("data", "");
			resultData.put("msg", "删除成功");
		} catch (Exception e) {
			e.getMessage();
			throw new Exception(e.getMessage());
		}

		return resultData;
	}

	/**
	 * 修改
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateRegister")
	@ResponseBody
	public PageData updateRegister() throws Exception {
		// 返回结果
		PageData result = new PageData();
		// 参数
		PageData pageData = getPageData();
		try {
			Map<String, Object> pageMap = UuidUtil.getPassword(pageData.getString("password"), pageData.getString("username"));
			pageData.put("salt", pageMap.get("uuid"));
			pageData.put("password", pageMap.get("password"));
			pageData.put("updateTime", DateUtil.getTime());
			
			PageData registerData = registerService.selectRegister(pageData);
			if (!CollectionUtils.isEmpty(registerData) && !registerData.getString("id").equals(pageData.getString("id"))) {
				result.put("code", "01");
				result.put("data", "");
				result.put("msg", "该账号已存在");
			}else {
				registerService.updateRegister(pageData);
				result.put("code", "00");
				result.put("data", "");
				result.put("msg", "保存成功");
			}
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;
	}

	/**
	 * 分页与查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userManageList")
	@ResponseBody
	public String userManageList() throws Exception {
		PageData pageData = getPageData();
		PageHelper.startPage(Integer.parseInt(pageData.getString("page")),
				Integer.parseInt(pageData.getString("limit")));// 页面的位置和展示的数据条目数
		String data = "";
		try {
			List<PageData> dataList = registerService.userManageList(pageData);
			PageInfo<PageData> pageInfo = new PageInfo<PageData>(dataList);
			for (PageData page : dataList) {
				if (page.get("createTime") != null && page.get("updateTime") != null) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String createTime = sdf.format(page.get("createTime"));
					String updateTime = sdf.format(page.get("updateTime"));
					page.put("createTime", createTime);
					page.put("updateTime", updateTime);
				}
			}
			data = DataUtil.getJsonData(pageInfo);
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new Exception(e.getMessage());
		}
		return data;
	}
	
	/**
	 *   查询数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showUser")
	@ResponseBody
	public PageData showUser() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {
			PageData data=	registerService.findShow(pageData);
			result.put("code", "00");
			result.put("data",data);
			result.put("uploadFile",getimg+data.getString("headImg"));//头像地址
			result.put("msg", "查询成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}
	
	@RequestMapping("/upload")
	@ResponseBody
	public PageData upload(@RequestParam() MultipartFile file) throws Exception {
		PageData resultData = new PageData();
		try {
			String fileName = FileUpload.fileUploadNew(file, upimg);
			resultData.put("code", "1");
			resultData.put("imgname",fileName);
			resultData.put("msg", "上传成功");
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new Exception(e.getMessage());
		}
		return resultData;
	}

}
